package com.waimai.service.imlp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waimai.Dao.DishMapper;
import com.waimai.Dto.DishDto;
import com.waimai.domain.Dish;
import com.waimai.domain.DishFlavor;
import com.waimai.service.DishFlavorService;
import com.waimai.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.rmi.log.LogInputStream;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImlp extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    DishFlavorService dishFlavorService;
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //先把数据封装到菜品表
        this.save(dishDto);
        //获得菜品ID
        Long id = dishDto.getId();

        List<DishFlavor> flavorlist =dishDto.getFlavors();
        flavorlist = flavorlist.stream().map((item)->{
            item.setDishId(id);
            return  item;
        }).collect(Collectors.toList());

        //保存到数据库
        dishFlavorService.saveBatch(flavorlist);
    }

    @Override
    public DishDto getWithFlavor(Long id) {
        Dish Id = this.getById(id);

        DishDto dishDto= new DishDto();
        BeanUtils.copyProperties(Id,dishDto);

        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper= new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,Id.getId() );
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    public void updataWithFlavor(DishDto dishDto) {

    }


}
